package com.jxs.android.sxyw;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.CaseInfo;
import com.jxs.android.sxyw.data.Person;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

public class CaseNextPerson extends AbstractActivity implements OnClickListener {

	private CaseInfo caseInfo;
	private ProgressDialog progressDialog = null;
	private Button submit;
	private LinearLayout nexPerson_ll;
	private TextView nexPerson_tv;
	private String user_id;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.next_person_submit);

		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}

		nexPerson_ll = (LinearLayout) findViewById(R.id.next_person_ll);
		nexPerson_tv = (TextView) findViewById(R.id.next_person);
		submit = (Button) findViewById(R.id.sumbit);

		if (null != getIntent() && null != getIntent().getExtras()) {
			caseInfo = (CaseInfo) getIntent().getExtras().get("caseInfo");
		}

		submit.setOnClickListener(this);
		nexPerson_ll.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == submit.getId()) {
			if (StringUtils.isEmpety(user_id)) {
				PopMsg.show("请选择接单人..");
				return;
			}
			sumbit(SelfApplication.getUserInfo().getTicket(), caseInfo, user_id);
		} else if (nexPerson_ll.getId() == v.getId()) {
			Intent intent = new Intent(this, WorkOrderNextPersonList.class);
			Bundle bundle = new Bundle();
			bundle.putString("method_name", "getMspStaffList");
			bundle.putString("case_no", caseInfo.getCase_no());
			bundle.putString("workOrderType", caseInfo.getOrderType().index + "");
			intent.putExtras(bundle);
			startActivityForResult(intent, 0);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			if (null != bundle) {
				Person person = (Person) bundle.getSerializable("person");
				user_id = person.getUser_id();
				this.nexPerson_tv.setText(person.getUser_name());
			}
		}
	}

	private int RESULT = 0x98765;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (msg.what == RESULT) {
				String status = msg.obj.toString();
				if ("0".equals(status)) {
					setResult(RESULT_OK);
					finish();
				} else if ("-1".equals(status)) {
					PopMsg.show("登录已过期,请重新登录");
					StartHelper.startActivity(Login.class);
					finish();
				} else if ("-2".equals(status)) {
					PopMsg.show("服务端异常");
				} else if ("-3".equals(status)) {
					PopMsg.show("输入参数无效");
				}
			}
		}

	};

	private void sumbit(final String ticket, final CaseInfo caseInfo, final String person_id) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "processWorkOrderToStaff";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("case_no", caseInfo.getCase_no());
				request.addProperty("user_id", person_id);
				request.addProperty("user_name", nexPerson_tv.getText().toString() + "");
				request.addProperty("workOrderType", caseInfo.getOrderType().index + "");
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						paserSubmit(envelope.getResponse().toString());
					}
				});
			}
		}).start();
	}

	private void paserSubmit(String stauts) {
		Message msg = new Message();
		msg.what = RESULT;
		msg.obj = stauts;
		handler.sendMessage(msg);
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.work_order_reply);
		initTopBarReturn(topBar);
		topBar.hindBtn();
	}

}
